﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>2.NFinal依赖注入设置</title>
	<script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
	<script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
	<link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
	<script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
	<h3>2.NFinal依赖注入设置</h3>
	<p>
		<b>1.声明接口和实现类</b>
		<pre class="brush: csharp">
            //1.定义Animal接口
            public interface IAnimal
            {
				string Name{get;set;}
                void Talk();
				void Talk(string word);
            }
            //2.定义实现该Animal接口的Dog类
            public class Dog : IAnimal
            {
				public string Name{get;set;}
				public Dog()
				{
				}
				public Dog(string Name)
				{
					this.Name=Name;
				}
                public void Talk()
                {
                    Console.WriteLine("汪汪！");
                }
				public void Talk(string word)
				{
					Console.WriteLine(Name+"说："+word);
				}
            }
            //3.定义实现该Animal接口的Cat类
            public class Cat : IAnimal
            {
				public string Name{get;set;}
				public Dog()
				{
				}
				public Dog(string Name)
				{
					this.Name=Name;
				}
                public void Talk()
                {
                    Console.WriteLine("喵喵！");
                }
				public void Talk(string word)
				{
					Console.WriteLine(Name+"说："+word);
				}
            }
        </pre>
	</p>
	<b>2.使用NFinal.DependencyInjection.ServiceCollection类</b><br/>
	<p>
		a.注入并执行无参数的初始化函数
		<pre class="brush: csharp">
		public static NFinal.DependencyInjection.ServiceCollection serviceCollection=new NFinal.DependencyInjection.ServiceCollection();
        public static void Main(string[] args)
        {
			//注入无参数的初始化函数
			serviceCollection.Set&lt;IAnimal>(typeof(Dog));
			//执行无参数的初始化函数并返回对象
			IAnimal animal = serviceCollection.Get&lt;IAnimal>();
			//执行并输出：汪汪！
			animal.Talk();
        }
    </pre>
	</p>
	<p>
		b.注入并执行带参数的初始化函数
		<pre class="brush: csharp">
		public static NFinal.DependencyInjection.ServiceCollection serviceCollection=new NFinal.DependencyInjection.ServiceCollection();
		public static void Main(string[] args)
		{
			//注入带参数的初始化函数
			serviceCollection.Set&lt;IAnimal,string>(typeof(Dog));
			//执行带参数的初始化函数并返回对象
			IAnimal animal = serviceCollection.Get&lt;IAnimal,string>("旺财");
			//执行并输出：旺财说：Hello!
			animal.Talk("Hello!");
		}
    </pre>
	</p>
	<p>
		c.使用重写参数进行注入
		<pre class="brush: csharp">
		public static NFinal.DependencyInjection.ServiceCollection serviceCollection=new NFinal.DependencyInjection.ServiceCollection();
		public static void Main(string[] args)
		{
			//注入并重写初始化函数，默认为重写
			serviceCollection.Set&lt;IAnimal>(typeof(Dog),true);
			//注入并重写初始化函数，默认为重写
			serviceCollection.Set&lt;IAnimal>(typeof(Cat),true);
			//执行带参数的初始化函数并返回对象
			IAnimal animal = serviceCollection.Get&lt;IAnimal,string>();
			//执行并输出：喵喵！
			animal.Talk();
		}
    </pre>
	</p>
	<p>
		d.使用非重写参数进行注入
		<pre class="brush: csharp">
		public static NFinal.DependencyInjection.ServiceCollection serviceCollection=new NFinal.DependencyInjection.ServiceCollection();
		public static void Main(string[] args)
		{
			//注入并重写初始化函数，默认为重写
			serviceCollection.Set&lt;IAnimal>(typeof(Dog),true);
			//注入并重写初始化函数，即该服务注册后，将不做任何改动。
			serviceCollection.Set&lt;IAnimal>(typeof(Cat),false);
			//执行带参数的初始化函数并返回对象
			IAnimal animal = serviceCollection.Get&lt;IAnimal,string>();
			//执行并输出：旺旺！
			animal.Talk();
		}
    </pre>
	</p>
</body>
</html>